package com.starblink.android.basic.base.activity

import android.os.Bundle
import androidx.annotation.CallSuper
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.base.mvvm.ViewModelFactory
import com.starblink.android.basic.ext.toBean
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.network.base.BaseApiConfig
import com.starblink.android.basic.network.base.BaseSiteSwitch
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.widget.ConfirmDialog
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.style.R
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.FetchSiteQuery
import com.starblink.rocketreserver.fragment.SiteVOF
import com.starblink.rocketreserver.type.SiteRequest
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.lang.reflect.ParameterizedType

/**
 * viewmodel 基础Activity
 */
abstract class AbsVMActivity<VM : BaseViewModel> : AbsActivity() {

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.clear()
    }

    protected lateinit var viewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewModel()
        initData()
        initView()
        initObservable()
        initLoad()
    }




    private fun initViewModel() {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val tp = type.actualTypeArguments[1]
            val tClass = tp as? Class<VM> ?: BaseViewModel::class.java
            viewModel = ViewModelProvider(
                this.viewModelStore,
                ViewModelFactory(this)
            )[tClass] as VM
        }
        viewModel.viewBaseLD.observe(this) {
            if (it == BaseViewModel.POP_OWNER) {
                popSelf()
            }
        }


    }

    /**
     * call before [initView]
     */
    @CallSuper
    open fun initData() {

    }

    @CallSuper
    open fun initView() {

    }

    @CallSuper
    open fun initObservable() {

    }

    @CallSuper
    open fun initLoad() {

    }


}